<?php
namespace Common\Model;
use Think\Model;

/**
 * Class MobileRegestModel
 * @package Common\Model
 * 移动端管理人员签到表
 */
class MobileRegestModel extends BaseModel{
    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'createtime desc,status asc';

    public function get_list_use_page($map=array(), $cur_page, $rows_num){
        $map = filter_query_map($map);
        if($map['uid'][1] != '%%'&& $map['uid'][1] != ''){
            $map['uid'][0] = 'eq';
            $map['uid'][1] = str_replace('%', '', $map['uid'][1]);
        }
        if($map['start'][1] != ''&& $map['end'][1] != ''){
            $map['start'][1] = str_replace('%', '', $map['start'][1]);
            $map['end'][1] = str_replace('%', '', $map['end'][1]);
            $where_str = 'createtime>=\''.$map['start'][1].'\' and createtime<=\''.$map['end'][1].'\'';
            unset($map['start']);
            unset($map['end']);
        }
        if(isset($map['sheshi'])){
            if(isset($map['sheshi_id'])){
                $map['sheshi_id'][1] = str_replace('%', '', $map['sheshi_id'][1]);;
                if($map['sheshi'][1]=='%1%'){
                    //泵站
                    $sheshi = D('PumpingStation')->queryObj($map['sheshi_id'][1], 'id');
                    $opr = $sheshi['pups_opr'];
                }
                if($map['sheshi'][1]=='%2%'){
                    $sheshi = D('Csdt')->queryObj($map['sheshi_id'][1], 'id');
                    $opr = $sheshi['csdt_opr'];
                }
                if($map['sheshi'][1]=='%3%'){
                    $sheshi = D('Gate')->queryObj($map['sheshi_id'][1], 'id');
                    $opr = $sheshi['gate_opr'];
                }
                if(!$opr){
                    $opr = '0';
                }
                $opr = clear_array_to_new_array(explode(',', $opr));
                $sheshi_where_str = '';
                if($where_str)
                    $where_str = '('.$where_str.') and ';
                for($i=0;$i<count($opr);$i++){
                    $sheshi_where_str .= 'uid='.$opr[$i];
                    if($i < count($opr)-1){
                        $sheshi_where_str .= ' or ';
                    }
                }
                $where_str = $where_str.'('.$sheshi_where_str.')';
//                echo $where_str;
            }
            unset($map['sheshi']);
            unset($map['sheshi_id']);
        }
        $admin_model = D('MobileAdmin');
        if($where_str){
            $count = $this->where($map)->where($where_str)->count();
            $list = $this->where($map)->where($where_str)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        }else{
            $count = $this->where($map)->count();
            $list = $this->where($map)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        }
        $befor_array = array();
        $nor_array = array();
        $today = date('Y-m-d').' 00:00:00';
        //提取迟到早退的
        foreach($list as $k => $v){
            if($list[$k]['status']<0&&$list[$k]['createtime']>$today){
                $befor_array[] = $v;
            }else{
                $nor_array[] = $v;
            }
        }

        $list = array_merge($befor_array, $nor_array);
        $map = array();
        foreach($list as $k => $v){
            if($v['status']==2||$v['status']==-2){
                $map['id'] = array('lt', $v['id']);
                $map['uid'] = array('eq', $v['uid']);
                $str = 'status=1 or status=-1';
                $last_work_record = $this->where($map)->where($str)->order('createtime desc')->find();
                $list[$k]['work_time'] = (strtotime($v['createtime'])-strtotime($last_work_record['createtime']))/(60*60);
                $list[$k]['work_time'] = round($list[$k]['work_time'], 2);
            }
            $person = $admin_model->queryObj($v['uid'], 'id');
            $list[$k]['realname'] = $person['realname'];
            $list[$k]['phone'] = $person['phone'];
            $list[$k]['status_text'] = sign_option($list[$k]['status'], false);
            $list[$k]['photo'] = '<a href="javascript:open_dialog(\''.$list[$k]['photo'].'\')">查看</a>';
        }
        return array('total'=>$count, 'list'=>$list, 'map'=>$map);
    }

    public function find_last_one_by_user($uid){
        $map['uid'] = array('eq', $uid);
        $one = $this->where($map)->order('createtime desc')->find();
        return $one?$one['status']:2;
    }

    public function bz_record($fy, $list){
        if(!S('mobile_admin')){
            $m = D('MobileAdmin');
            $s = $m->select();
            $count = count($s);
            $a = array();
            for($i=0;$i<$count;$i++){
                $a[$s[$i]['id']] = $s[$i];
            }
            S('mobile_admin', json_encode($a));
        }
        static $as;
        if(!$as){
            $as = json_decode(S('mobile_admin'), true);
        }
        foreach($list as $k => $item){
            $list[$k]['uid'] = $as[$item['uid']]['realname'];
        }
        return $list;
    }
}